﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TRicheControl.WinformControl
{
    public partial class RicheButtonTextBox : Control
    {
        private RicheTextBox mTextBox;

        private RicheButton mButton;

        private int mButtonWidth = 30;

        public RicheButtonTextBox()
        {
            InitializeComponent();

            mTextBox = new RicheTextBox();
            mTextBox.Name = "RicheTextBox";
            mTextBox.Location = new Point(0, 0);
            mTextBox.Dock = DockStyle.Fill;
            mTextBox.Text = "";

            mButton = new RicheButton();
            mButton.Name = "RicheButton";
            mButton.Size = new Size(mButtonWidth - 2, mTextBox.Height - 2);
            mButton.Location = new Point(mTextBox.Width - mButtonWidth + 1, 1);
            mButton.ForeColor = Color.White;
            mButton.Text = "...";
            mButton.Click += MButton_Click;

            Controls.Add(mButton);
            Controls.Add(mTextBox);
        }

        public new string Text
        {
            get
            {
                return mTextBox.Text;
            }
            set
            {
                mTextBox.Text = value;
            }
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            this.Bounds = new Rectangle(this.Bounds.Location, mTextBox.Size);
            mButton.Location = new Point(mTextBox.Width - mButtonWidth + 1, 1);
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
        }

        private void MButton_Click(object sender, EventArgs e)
        {
            if (mOnButtonClick != null)
            {
                mOnButtonClick(sender, e);
            }
        }

        #region Events

        private event EventHandler mOnButtonClick;

        /// <summary>
        /// 当按钮点击后触发的事件
        /// </summary>
        public event EventHandler OnButtonClick
        {
            add { mOnButtonClick += value; }
            remove { mOnButtonClick += value; }
        }

        #endregion
    }
}
